import Vue from 'vue';
import store from '../store/index.js';

Vue.prototype.price = function(price) {
	let num = Math.round((price*100))/100;
	if(!( num + "".split('.')[1])){
		return num.toFixed(2);
	}else{
		return num + "";
	}
};

Vue.prototype.timestamp = function(timestamp) {
	let d = new Date(timestamp * 1000); //根据时间戳生成的时间对象
	let year = d.getFullYear();
	let month = d.getMonth() + 1;
	if (month < 10) {
		month = "0" + month
	}
	let date = d.getDate();
	if (date < 10) {
		date = "0" + date
	}
	let hours = d.getHours();
	if (hours < 10) {
		hours = "0" + hours
	}
	let minutes = d.getMinutes();
	if (minutes < 10) {
		minutes = "0" + minutes
	}
	let seconds = d.getSeconds();
	if (seconds < 10) {
		seconds = "0" + seconds
	}
	let dateTime = year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds
	return dateTime;
};

/**
 * js生成的时间戳转日期
 */
Vue.prototype.jstimestamp = function(timestamp) {
	let d = new Date(timestamp); //根据时间戳生成的时间对象
	let year = d.getFullYear();
	let month = d.getMonth() + 1
	if (month < 10) {
		month = "0" + month
	}
	let date = d.getDate();
	if (date < 10) {
		date = "0" + date
	}
	let hours = d.getHours();
	if (hours < 10) {
		hours = "0" + hours
	}
	let minutes = d.getMinutes();
	if (minutes < 10) {
		minutes = "0" + minutes
	}
	let seconds = d.getSeconds();
	if (seconds < 10) {
		seconds = "0" + seconds
	}
	let dateTime = year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds;
	return dateTime;
};

/**
 * 时间格式调整
 */
Vue.prototype.timereplace = function(data){
	let time  = data + "";
	if(time.length == 8){
		return time.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3');
	}else if(time.length == 14){
		return time.replace(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/, '$1-$2-$3 $4:$5:$6');
	}else{
		throw new Error('请传入一个8位或者14位的数字');
	}
};